<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * A base controller for all controllers. here we set various variables and tasks to avoid redundantly doing it throughout codebase
 *
 * @author 		Khiem Pham <khiemktqd@gmail.com>
 * @date 		05/08/2013
 */
class MY_Controller extends CI_Controller {

    var $menu_config_0 = array('', '', '', '', '', '', '', '', '', '', '');
    var $menu_config_1 = array('', 'active', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
    var $menu_config_2 = array('', '', 'active', '', '', '', '', '', '', '', '', '', '', '', '', '');
    var $menu_config_3 = array('', '', '', 'active', '', '', '', '', '', '', '', '', '', '', '', '');
    var $menu_config_4 = array('', '', '', '', 'active', '', '', '', '', '', '', '', '', '', '', '');
    var $menu_config_5 = array('', '', '', '', '', 'active', '', '', '', '', '', '', '', '', '', '');
    var $menu_config_6 = array('', '', '', '', '', '', 'active', '', '', '', '', '', '', '', '', '');
    var $menu_config_7 = array('', '', '', '', '', '', '', 'active', '', '', '', '', '', '', '', '');
    var $menu_config_8 = array('', '', '', '', '', '', '', '', 'active', '', '', '', '', '', '', '');
    var $menu_config_9 = array('', '', '', '', '', '', '', '', '', 'active', '', '', '', '', '', '');
    var $menu_config_10 = array('', '', '', '', '', '', '', '', '', '', 'active', '', '', '', '', '');
    var $menu_config_11 = array('', '', '', '', '', '', '', '', '', '', '', 'active', '', '', '', '');
    var $menu_config_12 = array('', '', '', '', '', '', '', '', '', '', '', '', 'active', '', '', '');
    var $menu_config_13 = array('', '', '', '', '', '', '', '', '', '', '', '', '', 'active', '', '');
    var $menu_config_14 = array('', '', '', '', '', '', '', '', '', '', '', '', '', '', 'active', '');
    var $menu_config_15 = array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'active');
    protected $data;
    protected $permission;
    protected $configs;
    protected $language_id;
    protected $language;
    var $ignore = array(
        'authenticate', 'home', 'errorpermission'
    );

    public function __construct() {

        parent::__construct();

        $this->load->model('user_model', 'user');
        $this->load->model('role_model', 'role');
        $this->load->model('languages_model', 'languages');

        $this->configs = $this->configs_model->getConfigs();


        $language = $this->session->userdata('language');
        
        if (!$language)
            $this->language_id = $this->configs['language'];
        else
            $this->language_id = $language;
        $this->language = $this->languages->getLanguageById($this->language_id);
        
        $this->lang->load($this->language->filename, $this->language->directory);
        
        $this->data['languages'] = $this->languages->getLanguages();
        $this->data['language_id'] = $this->language_id;

        $user_session = $this->session->userdata('user');
        if ($user_session) {
            $permissions = $this->role->getUserRole($user_session['role']);
            $permission_list = ($permissions['permission']);

            if (is_array($permission_list)) {
                foreach ($permission_list as $key => $value) {
                    $this->permission[$key] = $value;
                }
            }
        }
    }
    
    public function load_lang($file){
        
        $lang = $this->languages->getLanguageById($this->language_id);
        $this->lang->load($file, $lang->directory);
    }

    public function permission() {
        if (!in_array($this->router->fetch_class(), $this->ignore) && !$this->hasPermission('access', $this->router->fetch_class())) {
            return redirect(admin_url('errorpermission'));
        }
    }

    public function hasPermission($value) {

        if (isset($this->permission[$value])) {
            return in_array($this->router->fetch_class(), $this->permission[$value]);
        } else {
            return false;
        }
    }

    function load_view($body_view = null, $tpl_view = 'default', $tpl_menu = '') {
        if (!is_null($body_view)) {
            if (file_exists(APPPATH . 'views/' . $tpl_view . '/' . $body_view)) {
                $body_view_path = $tpl_view . '/' . $body_view;
            } else if (file_exists(APPPATH . 'views/' . $tpl_view . '/' . $body_view . '.php')) {
                $body_view_path = $tpl_view . '/' . $body_view . '.php';
            } else if (file_exists(APPPATH . 'views/' . $body_view)) {
                $body_view_path = $body_view;
            } else if (file_exists(APPPATH . 'views/' . $body_view . '.php')) {
                $body_view_path = $body_view . '.php';
            } else {
                show_error('Unable to load the requested file: ' . $tpl_name . '/' . $view_name . '.php');
            }
            $body = $this->load->view($body_view_path, $this->data, TRUE);
            if (is_null($this->data)) {
                $this->data = array('body' => $body);
            } else if (is_array($this->data)) {
                $this->data['body'] = $body;
            } else if (is_object($this->data)) {
                $this->data->body = $body;
            }
        }
        if ($tpl_menu)
            $this->data['menu'] = $this->load->view('templates/' . $tpl_menu, $this->data, TRUE);
        $this->load->view('templates/' . $tpl_view, $this->data);
    }

}